﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    /*
     * It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

     * */
    class Problem52 : IProblem
    {
        public string Calculate()
        {
            //taktika: bruteforce
            //jedino prva znamenka treba bit jedan, druga maksimalno 6 i to je to

            //n je max množenja
            int n = 6;

            long number = 0;

            while (true)
            {
                number++;
                
                int numDigits = CommonFunctions.NumberOfDigits(number);
                int numMaxDigits = CommonFunctions.NumberOfDigits(n * number);

                //idemo na slijedeće brojeve
                if (numMaxDigits > numDigits)
                {
                    number = (long)Math.Pow(10, numDigits) - 1;
                    continue;
                }

                var originalDigits = CommonFunctions.GetDigits(number).ToList();

                bool found = true;

                for (int i = 2; i <= n; i++)
                {
                    var digits = CommonFunctions.GetDigits(i * number);
                    var tempDigits = new List<int>(originalDigits);
                    
                    
                    foreach (int digit in digits)
                    {
                        if (!tempDigits.Remove(digit))
                        {
                            found = false;
                            break;
                        }
                    }
                }

                if (found)
                    break;
            }

            return number.ToString();
        }
    }
}
